package com.taxshare.seller.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.text.MessageFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


/**
 * HttpUtil
 *
 * @author admin
 * @date 2019/11/20
 */
@SuppressWarnings("all")
public class HttpUtils {

    public static final String ENCODING = "UTF-8";

    /**
     * 模拟请求
     *
     * @param url 资源地址
     * @param map 参数列表
     * @return
     * @throws ParseException
     * @throws IOException
     */
    public static String sendPost(String url, Map<String, Object> map) throws IOException {
        String body = "";
        //创建httpclient对象
        CloseableHttpClient client = HttpClients.createDefault();
        //创建post方式请求对象
        HttpPost httpPost = new HttpPost(url);
        //装填参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        if (map != null) {
            for (Entry<String, Object> entry : map.entrySet()) {
                nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
            }
        }
        //设置参数到请求对象中
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, ENCODING));
        System.out.println("请求地址：" + url);
        System.out.println("请求参数：" + nvps.toString());
        //设置header信息 【***************注意:请求类型Content-type的值设置********************】
        httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
        httpPost.setHeader("Accept-Charset", "utf-8");
        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
        //执行请求操作
        CloseableHttpResponse response = client.execute(httpPost);
        //获取结果实体
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            //按指定编码转换结果实体为String类型
            body = EntityUtils.toString(entity, ENCODING);
        }
        EntityUtils.consume(entity);
        //释放链接
        response.close();
        return body;
    }

    public static String sendPostJson(String url, Map<String, Object> map) throws IOException {
        String body = "";
        //创建httpclient对象
        CloseableHttpClient client = HttpClients.createDefault();
        //创建post方式请求对象
        HttpPost httpPost = new HttpPost(url);
        //设置超时时间
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(5000).setConnectionRequestTimeout(15000)
                .setSocketTimeout(30000).build();
        httpPost.setConfig(requestConfig);
        //JSON格式封装
        JSONObject jsonParam = new JSONObject(map);
        //解决中文乱码问题
        StringEntity entity = new StringEntity(jsonParam.toString(), ENCODING);
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        //执行请求操作
        CloseableHttpResponse response = client.execute(httpPost);
        //获取结果实体
        HttpEntity responseEntity = response.getEntity();
        if (responseEntity != null) {
            //指定编码转换结果实体为String类型
            body = EntityUtils.toString(responseEntity, ENCODING);
        }
        EntityUtils.consume(responseEntity);
        //释放链接
        response.close();
        return body;
    }
}
